package v1alpha1

import "reflect"

type Match struct {
	Operations []string `json:"operations,omitempty"`

	// +kubebuilder:validation:Enum=*;Cluster;Namespaced
	Scope string `json:"scope,omitempty"`

	UserInfo UserInfo `json:"userInfo,omitempty"`
	Kinds    []Kind   `json:"kinds"`

	Name Name `json:"name,omitempty"`

	NamespaceSelector  NamespaceSelector `json:"namespaceSelector,omitempty"`
	ExcludedNamespaces Namespace         `json:"excludedNamespaces,omitempty"`
	IncludedNamespaces Namespace         `json:"namespaces,omitempty"`

	AnnotationSelector AnnotationSelector `json:"annotationSelector,omitempty"`
	LabelSelector      LabelSelector      `json:"labelSelector,omitempty"`

	MatchConditions MatchConditions `json:"matchConditions,omitempty"`
}

type Namespace struct {
	Values []string `json:"values,omitempty"`
	Regex  []string `json:"regex,omitempty"`
}

type Name struct {
	Value string `json:"value,omitempty"`
	Regex string `json:"regex,omitempty"`
}

type MatchConditions struct {
	Object    []Obj `json:"object,omitempty"`
	OldObject []Obj `json:"oldObject,omitempty"`
}

type Obj struct {
	Path string `json:"path"`

	// +kubebuilder:validation:Enum=MustExist;MustNotExist
	Condition string `json:"condition"`
}

type Kind struct {
	Kind      []string `json:"kinds,omitempty"`
	ApiGroups []string `json:"apiGroups,omitempty"`
}

type UserInfo struct {
	Username      string `json:"username,omitempty"`
	UsernameRegex string `json:"usernameRegex,omitempty"`
}

type NamespaceSelector struct {
	MatchExpressions []MatchExpression `json:"matchExpressions"`
}

type AnnotationSelector struct {
	MatchExpressions []MatchExpression `json:"matchExpressions"`
}

type LabelSelector struct {
	MatchExpressions []MatchExpression `json:"matchExpressions"`
}

type MatchExpression struct {
	Key string `json:"key"`

	// +kubebuilder:validation:Enum=In;NotIn;Exists;DoesNotExist
	Operator string   `json:"operator"`
	Values   []string `json:"values,omitempty"`
}

func (in *Match) IsEmpty() bool {
	return reflect.DeepEqual(*in, Match{})
}
